package com.gujiangbo.juc;

import sun.misc.Unsafe;

import java.util.concurrent.TimeUnit;

/**
 * @Author: gujiangbo.co
 * @Contact: 646832012@qq.com
 * @Date: 2022/6/5 20:09
 * @Version: 1.0
 * @Description: 死锁
 */
public class JavaDeadLockDemo {

    public static void main(String[] args) {

        final Object objA = new Object();
        final Object objB = new Object();
        new Thread(() -> {
            synchronized (objA) {
                System.out.println(Thread.currentThread().getName() + "持有锁A");
                //暂停1秒钟线程
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (objB) {
                    System.out.println(Thread.currentThread().getName() + "持有锁B");
                }
            }
        }, "a").start();
        new Thread(() -> {
            synchronized (objB){
                System.out.println(Thread.currentThread().getName() + "持有锁B");
                //暂停1秒钟线程
                try{TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
                synchronized (objA){
                    System.out.println(Thread.currentThread().getName() + "持有锁");
                }
            }
        }, "b").start();
    }
}
